<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检查部门" prop="inspectionDepartment">
        <el-input
          v-model="queryParams.inspectionDepartment"
          placeholder="请输入检查部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查人员" prop="inspectorName">
        <el-input
          v-model="queryParams.inspectorName"
          placeholder="请输入检查人员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产主体" prop="productionEntity">
        <el-input
          v-model="queryParams.productionEntity"
          placeholder="请输入生产主体"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人名称" prop="legalPersonName">
        <el-input
          v-model="queryParams.legalPersonName"
          placeholder="请输入法人名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="districtCounty">
        <el-input
          v-model="queryParams.districtCounty"
          placeholder="请输入区/县"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="镇/乡" prop="townVillage">
        <el-input
          v-model="queryParams.townVillage"
          placeholder="请输入镇/乡"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查地点" prop="inspectionLocation">
        <el-input
          v-model="queryParams.inspectionLocation"
          placeholder="请输入检查地点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格证开具是否规范" prop="certificateStandard">
        <el-input
          v-model="queryParams.certificateStandard"
          placeholder="请输入合格证开具是否规范"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否使用禁用农药、兽药" prop="prohibitedChemicalsUsed">
        <el-input
          v-model="queryParams.prohibitedChemicalsUsed"
          placeholder="请输入是否使用禁用农药、兽药"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查时间" prop="inspectionTime">
        <el-date-picker clearable
          v-model="queryParams.inspectionTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择检查时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nongjt:checkRecords:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:checkRecords:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:checkRecords:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['nongjt:checkRecords:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkRecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="检查部门" align="center" prop="inspectionDepartment" />
      <el-table-column label="检查人员" align="center" prop="inspectorName" />
      <el-table-column label="生产主体" align="center" prop="productionEntity" />
      <el-table-column label="法人名称" align="center" prop="legalPersonName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="districtCounty" />
      <el-table-column label="镇/乡" align="center" prop="townVillage" />
      <el-table-column label="检查地点" align="center" prop="inspectionLocation" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="合格证开具是否规范" align="center" prop="certificateStandard" />
      <el-table-column label="是否使用禁用农药、兽药" align="center" prop="prohibitedChemicalsUsed" />
      <el-table-column label="检查意见" align="center" prop="inspectionOpinion" />
      <el-table-column label="检查记录照片" align="center" prop="inspectionPhotos" />
      <el-table-column label="检查时间" align="center" prop="inspectionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inspectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:checkRecords:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:checkRecords:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农产品检查记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="checkRecordsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检查部门" prop="inspectionDepartment">
          <el-input v-model="form.inspectionDepartment" placeholder="请输入检查部门" />
        </el-form-item>
        <el-form-item label="检查人员" prop="inspectorName">
          <el-input v-model="form.inspectorName" placeholder="请输入检查人员" />
        </el-form-item>
        <el-form-item label="生产主体" prop="productionEntity">
          <el-input v-model="form.productionEntity" placeholder="请输入生产主体" />
        </el-form-item>
        <el-form-item label="法人名称" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区/县" prop="districtCounty">
          <el-input v-model="form.districtCounty" placeholder="请输入区/县" />
        </el-form-item>
        <el-form-item label="镇/乡" prop="townVillage">
          <el-input v-model="form.townVillage" placeholder="请输入镇/乡" />
        </el-form-item>
        <el-form-item label="检查地点" prop="inspectionLocation">
          <el-input v-model="form.inspectionLocation" placeholder="请输入检查地点" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="合格证开具是否规范" prop="certificateStandard">
          <el-input v-model="form.certificateStandard" placeholder="请输入合格证开具是否规范" />
        </el-form-item>
        <el-form-item label="是否使用禁用农药、兽药" prop="prohibitedChemicalsUsed">
          <el-input v-model="form.prohibitedChemicalsUsed" placeholder="请输入是否使用禁用农药、兽药" />
        </el-form-item>
        <el-form-item label="检查意见" prop="inspectionOpinion">
          <el-input v-model="form.inspectionOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检查时间" prop="inspectionTime">
          <el-date-picker clearable
            v-model="form.inspectionTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择检查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择记录创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CheckRecords">
import { listCheckRecords, getCheckRecords, delCheckRecords, addCheckRecords, updateCheckRecords } from "@/api/nongjt/checkRecords"

const { proxy } = getCurrentInstance()

const checkRecordsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inspectionDepartment: null,
    inspectorName: null,
    productionEntity: null,
    legalPersonName: null,
    contactPhone: null,
    province: null,
    city: null,
    districtCounty: null,
    townVillage: null,
    inspectionLocation: null,
    productName: null,
    certificateStandard: null,
    prohibitedChemicalsUsed: null,
    inspectionOpinion: null,
    inspectionPhotos: null,
    inspectionTime: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    inspectionDepartment: [
      { required: true, message: "检查部门不能为空", trigger: "blur" }
    ],
    inspectorName: [
      { required: true, message: "检查人员不能为空", trigger: "blur" }
    ],
    productionEntity: [
      { required: true, message: "生产主体不能为空", trigger: "blur" }
    ],
    legalPersonName: [
      { required: true, message: "法人名称不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    province: [
      { required: true, message: "省不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "市不能为空", trigger: "blur" }
    ],
    districtCounty: [
      { required: true, message: "区/县不能为空", trigger: "blur" }
    ],
    inspectionLocation: [
      { required: true, message: "检查地点不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "产品名称不能为空", trigger: "blur" }
    ],
    inspectionTime: [
      { required: true, message: "检查时间不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询农产品检查记录列表 */
function getList() {
  loading.value = true
  listCheckRecords(queryParams.value).then(response => {
    checkRecordsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inspectionDepartment: null,
    inspectorName: null,
    productionEntity: null,
    legalPersonName: null,
    contactPhone: null,
    province: null,
    city: null,
    districtCounty: null,
    townVillage: null,
    inspectionLocation: null,
    productName: null,
    certificateStandard: null,
    prohibitedChemicalsUsed: null,
    inspectionOpinion: null,
    inspectionPhotos: null,
    inspectionTime: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("checkRecordsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加农产品检查记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCheckRecords(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改农产品检查记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkRecordsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheckRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCheckRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除农产品检查记录编号为"' + _ids + '"的数据项？').then(function() {
    return delCheckRecords(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/checkRecords/export', {
    ...queryParams.value
  }, `checkRecords_${new Date().getTime()}.xlsx`)
}

getList()
</script>
